package tool.soft

import java.io.File
import akka.stream.Materializer
import dao._
import implicits.Implicits._
import models.Tables
import models.Tables._
import org.joda.time.DateTime
import play.api.data.FormBinding
import play.api.libs.ws.WSClient
import play.api.mvc.{AnyContent, Request}
import play.twirl.api.Html
import shared.Pojo._
import tool.Pojo._
import tool.plot._
import tool.soft.AllSoft.SoftBT
import tool.softT.BorutaSoftBT
import tool._

import scala.language.postfixOps
import play.api.i18n.Messages
import utils.Utils
import upickle.default._

import scala.collection.immutable.SeqMap

/**
 * Created by yz on 15/9/2020
 */
object BorutaSoft extends BorutaSoftBT with SoftBT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit matrixFileDataDao: MatrixFileDataDao, messages: Messages): List[FileInfoData] = {
    List(
      FileInfoData("Decision_Boxplot.pdf", messages("decisionBoxplot")),
      FileInfoData("Decision_Info.csv", messages("decisionInfo")),
    )
  }

  override def resultDeal(workspaceDir: File, resultDir: File)(implicit ws: WSClient, materializer: Materializer): Unit = {
    Tool.resultDeal(workspaceDir, resultDir)
  }

  override def produceConfigFile(workspaceDir: File, index: Int)(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    super.produceConfigFile(workspaceDir, index)
    Utils.t2ConfigFile(workspaceDir, data)
    Utils.t2ConfigFile(data, Tool.getCalculateConfigJsonFile(workspaceDir))
  }

  override def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.mission.borutaResult(missionId)
  }

  def getArg(implicit request: Request[AnyContent], messages: Messages, formBinding: FormBinding): ujson.Value = {
    val scaleMethodMap=Tool.getScaleMethodMap
    ujson.Obj(messages("scaleMethod") ->scaleMethodMap(data.scaleMethod), messages("maxNumberOfRuns") -> data.maxRuns,
      messages("pValueCutoff") -> data.pValue)
  }

  override def getHtml(missionName: String, missionId: Option[Int])(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.soft.boruta(missionName, missionId)
  }

  override def getMission(implicit request: Request[AnyContent], matrixFileDataDao: MatrixFileDataDao, messages: Messages, formBinding: FormBinding): Tables.MissionRow = {
    val initInput = List(data.file, data.groupFile)
    val argJson = getArg
    MissionRow(id = 0, missionName = s"${data.missionName}", kind = nameStr, userId = userId,
      startTime = new DateTime(), state = "preparing", arg = argJson, input = initInput)
  }

  override def produceInputFile(workspaceDir: File)(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceRawMetaboliteFile(userId, data.file, rawDataFile)

    val originalGroupFile = new File(workspaceDir, "group.txt")
    Tool.produceRawGroupFile(userId, data.groupFile, rawDataFile, originalGroupFile)

  }

  override def data(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    FormTool.borutaForm.bindFromRequest().get
  }


}

